/**
 * 
 */
package vehicleController;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.sql.SQLException;
import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ResourceBundle;

import org.apache.commons.lang3.StringUtils;

import constants.Constants;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.paint.Color;
import userController.Navigator;
import validator.Validator;
import vehicleEntity.PurchasedVehicleDisplay;
import vehicleEntity.PurchasedVehicleEntity;
import vehicleLogic.PurchseLogic;


/**
 * @author Sashiraj Dhanarajan
 *
 */
public class PurchaseVehicleController implements Initializable{

	@FXML
	TableColumn<PurchasedVehicleDisplay, Integer> vehicleId;

	@FXML
	TableColumn<PurchasedVehicleDisplay, Date> purchasedDate;

	@FXML
	TableColumn<PurchasedVehicleDisplay, Date> manufDate;

	@FXML
	TableColumn<PurchasedVehicleDisplay, Double> vehicleIns;

	@FXML
	TableColumn<PurchasedVehicleDisplay, Double> amount;

	@FXML
	TableColumn<PurchasedVehicleDisplay, String> VehicleName;

	@FXML
	TableColumn<PurchasedVehicleDisplay, String> RegNo;

	@FXML
	TableColumn<PurchasedVehicleDisplay, String> VIN;

	@FXML
	TableColumn<PurchasedVehicleDisplay, String> vehicleClass;

	@FXML
	TableColumn<PurchasedVehicleDisplay, String> vehicleType;


	@FXML
	TableView<PurchasedVehicleDisplay> purchaselist;	

	@FXML
	private Label message;

	@FXML
	private TextField searchvehicleId;

	@FXML
	private TextField addVehicleID;

	@FXML
	private DatePicker addPurchasedDate;

	@FXML
	private DatePicker addManufDate;

	@FXML
	private TextField addVehicleIns;

	@FXML
	private TextField addAmount;

	@FXML
	private TextField addVehicleName;

	@FXML
	private TextField addRegNo;

	@FXML
	private TextField addVIN;

	@FXML
	private ComboBox<String> addVehicleClass;

	@FXML
	private ComboBox<String> addVehicleType;

	@FXML
	private ComboBox<String> addVehicleSpec;

	@FXML
	private ComboBox<String> addVehicleTransmission;

	@FXML
	private Label addmessage;

	@FXML
	private TextField editVehicleId;

	@FXML
	private DatePicker editPurchasedDate;

	@FXML
	private DatePicker editManufDate;

	@FXML
	private TextField editVehicleIns;

	@FXML
	private TextField editAmount;

	@FXML
	private TextField editVehicleName;

	@FXML
	private TextField editRegNo;

	@FXML
	private TextField editVIN;

	@FXML
	private ComboBox<String> editVehicleClass;

	@FXML
	private ComboBox<String> editVehicleType;

	@FXML
	private ComboBox<String> editVehicleSpec;

	@FXML
	private ComboBox<String> editVehicleTransmission;

	@FXML
	private Label editMessage;

	private static int page=0;

	private static ObservableList<PurchasedVehicleDisplay> list;

	private static PurchasedVehicleEntity purchaseentity;

	private static PurchasedVehicleDisplay pvd;

	private static int vId;

	/**
	 * This method fetches Purchased vehicle details 
	 * @pre The Purchase button is pressed
	 * @post Display the purchased vehicle list
	 */
	public void fetchPurchasedVehicles(){
		page = -2;
		PurchseLogic pl = new PurchseLogic();
		list=FXCollections.observableArrayList();
		try {
			list=pl.fetchPurchasedVehiclesDetails();
			Navigator.setInitailize(true);
			Navigator.loadStackPane(Navigator.MANAGERVEHICLEPURCHASEFXML);
		} catch (Exception e) {
			e.printStackTrace();
			// exception frame------------------------------------
		}

	}

	/**
	 * This method allows a selected row from purchase list to be
	 * edited or it allows to search a vehicle details based on vehicleId
	 * @param event
	 * @pre Edit button is pressed in the Purchase list screen
	 * @Post Display the screen to search a vehicle with vehicleId
	 */
	@FXML
	public void editPurchase(ActionEvent event){
		// load search frame
		page=1;
		ObservableList<PurchasedVehicleDisplay> selection = purchaselist.getSelectionModel().getSelectedItems();
		if(selection.size()>0)
		{ page=3;
		pvd=selection.get(0);
		System.out.println("PVD now has "+pvd.getVehicleId());
		PurchseLogic purchaselogic = new PurchseLogic();
		try {
			purchaseentity = purchaselogic.fetchPurchasedVehicleDetail(pvd.getVehicleId());
			vId = pvd.getVehicleId();
			Navigator.loadStackPane(Navigator.MANAGERVEHICLEPURCHASEEDITFORMFXML); 
			return;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		}
		System.out.println("Purchase entity is setting to null");
		purchaseentity=null;
		pvd=null;
		System.out.println("pur "+purchaseentity + " page "+page);
		Navigator.loadStackPane(Navigator.MANAGERVEHICLEPURCHASEEDITFXML);		

	}
	/**
	 * This method fetches all the vehicle details based on vehicleId
	 * @param event
	 * @pre No row should selected in the purchase list screen
	 * @post Display vehicle details to be edited
	 */

	@FXML
	public void searchPurchasedVehicle(ActionEvent event){
		page = 3;	
		message.setVisible(false);

		if(searchvehicleId.getText().trim().isEmpty())
		{
			message.setText("Please Enter the VehicleId number");
			message.setVisible(true);
			return;
		}
		if(!Validator.validateStringContainsInteger(searchvehicleId.getText().trim())){
			message.setText("Enter only integer value for vehicleId");
			message.setVisible(true);
			return;
		}

		vId = Integer.parseInt(searchvehicleId.getText().trim());
		PurchseLogic purchaselogic = new PurchseLogic();
		/*purchaseentity = new PurchasedVehicleEntity();*/
		try {
			purchaseentity = purchaselogic.fetchPurchasedVehicleDetail(vId);

			if(purchaseentity==null)
			{
				message.setText("Entered VehicleId not found");
				message.setVisible(true);
				return;

			}	
			Navigator.loadStackPane(Navigator.MANAGERVEHICLEPURCHASEEDITFORMFXML); 
		} catch (SQLException e) {
			e.printStackTrace();
			message.setVisible(true);
			message.setText("DB exception occurred pleasse try again later");
			return;
		}
		//purchaseentity = null;

	}

	/**
	 * This method save the updated values into the database
	 * @param event
	 * @throws SQLException
	 * @pre values should be edited and update button should be pressed
	 * @post Edited values updated into the database
	 */
	@FXML
	public void saveUpdatedValue(ActionEvent event){
		editMessage.setVisible(false);
		PurchseLogic purchaselogic = new PurchseLogic();
		try {
			if(editVIN.getText().trim().isEmpty())
			{
				editMessage.setText("Please Enter the VIN number");
				editMessage.setVisible(true);
				return;
			}
			else if(!purchaseentity.getVin().equals(editVIN.getText())){
				if(!purchaselogic.checkVin(editVIN.getText())){
					editMessage.setText("Entered VIN already exists");
					editMessage.setVisible(true);
					return;
				}
			}

			if(editPurchasedDate.getValue() == null)
			{
				editMessage.setText("Please enter the Purchased date");
				editMessage.setVisible(true);
				return;

			}
			if(editPurchasedDate.getValue().isAfter(LocalDate.now())) 
			{
				editMessage.setText("Please enter the Purchased date less "
						+ "than or equal to current date");
				editMessage.setVisible(true);
				return;
			}
			if(editManufDate.getValue() == null)
			{
				editMessage.setText("Please enter the Manufacture date");
				editMessage.setVisible(true);
				return;
			}

			if(editManufDate.getValue().isAfter(LocalDate.now())) 
			{
				System.out.println("future date");
				editMessage.setText("Please enter the Manufacture date less "
						+ "than or equal to current date");
				editMessage.setVisible(true);
				return;
			}
			if(editPurchasedDate.getValue().isBefore(editManufDate.getValue()) )
			{
				System.out.println("before");
				editMessage.setText("Please enter the Purchased date greater than or equal to manufacture date");
				editMessage.setVisible(true);
				return;
			} 

			LocalDate purchaseDate = editPurchasedDate.getValue();
			Instant instant = purchaseDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
			Date res = Date.from(instant);		
			purchaseentity .setPurchasedDate(res);

			LocalDate manufDate = editManufDate.getValue();
			Instant instant1 = manufDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
			Date res1 = Date.from(instant1);		
			purchaseentity .setManufDate(res1);

			purchaseentity.setVehicleId(vId);
			if(editVehicleIns.getText().trim().isEmpty())
			{
				editMessage.setText("Please Enter the Vehicle Insurance amount value");
				editMessage.setVisible(true);
				return;
			}
			else if (!Validator.validateStringContainsDouble(editVehicleIns.getText().trim()))
			{
				System.out.println("Enter valid entry for vehicle insurance");
				editMessage.setText("Enter valid entry for vehicle insurance");
				editMessage.setVisible(true);
				return;
			}
			if (editAmount.getText().trim().isEmpty())
			{
				editMessage.setText("Please Enter the Purchased amount value");
				editMessage.setVisible(true);
				return;	
			}
			else if(!Validator.validateStringContainsDouble(editAmount.getText().trim()))
			{
				System.out.println("Enter valid entry for Purchased Amount");
				editMessage.setText("Enter valid entry for Purchased Amount");
				editMessage.setVisible(true);
				return;
			}
			if(editVehicleName.getText().trim().isEmpty())
			{
				editMessage.setText("Please Enter the Vehicle Name");
				editMessage.setVisible(true);
				return;
			}
			else if (editRegNo.getText().trim().isEmpty())
			{
				editMessage.setText("Please Enter the RegNo");
				editMessage.setVisible(true);
				return;	
			}
			double vehicleIns = Validator.formatDouble(Double.parseDouble(editVehicleIns.getText().trim()));
			double addamount  = Validator.formatDouble(Double.parseDouble(editAmount.getText().trim()));
			purchaseentity.setVehicleIns(vehicleIns);
			purchaseentity.setAmount(addamount);
			//purchaseentity .setVehicleIns(new Double(editVehicleIns.getText().trim()));
			//purchaseentity .setAmount(new Double(editAmount.getText().trim()));
			purchaseentity .setVehicleName(editVehicleName.getText().trim());
			purchaseentity .setRegNo(editRegNo.getText().trim());
			purchaseentity .setVin(editVIN.getText().trim());
			if(editVehicleClass.getValue() != null && !StringUtils.isEmpty(editVehicleClass.getValue())){
				purchaseentity .setVehicleClass(editVehicleClass.getValue());
			}
			else{
				editMessage.setText("Please select vehicle class");
				editMessage.setVisible(true);
				return;
			}
			if(editVehicleType.getValue() != null && !StringUtils.isEmpty(editVehicleType.getValue())){
				purchaseentity .setVehicleType(editVehicleType.getValue());
			}
			else{
				editMessage.setText("Please select vehicle Type");
				editMessage.setVisible(true);
				return;
			}

			if(editVehicleSpec.getValue() != null && !StringUtils.isEmpty(editVehicleSpec.getValue())){
				purchaseentity .setSpec(editVehicleSpec.getValue());
			}
			else{
				editMessage.setText("Please select vehicle Specification");
				editMessage.setVisible(true);
				return;
			}

			if(editVehicleTransmission.getValue() != null && !StringUtils.isEmpty(editVehicleTransmission.getValue())){
				purchaseentity .setTransmission(editVehicleTransmission.getValue());
			}
			else{
				editMessage.setText("Please select vehicle Transmission");
				editMessage.setVisible(true);
				return;
			}
			String msg = purchaselogic.saveUpdatedValue(purchaseentity);
			editMessage.setText(msg);
			editMessage.setTextFill(Color.GREEN);
			editMessage.setVisible(true);
		} catch (SQLException e) {
			e.printStackTrace();
			editMessage.setText("DB error occurred. Update failed.");
		}		


	}
	/**
	 * This method is used for adding new vehicle details to the inventory
	 * 
	 * @param event
	 * @pre The Add vehicle button is pressed
	 * @post Display the screen with vehicle details to be entered
	 */

	@FXML
	public void add(ActionEvent event){

		page=2;
		Navigator.loadStackPane(Navigator.MANAGERVEHICLEPURCHASEADDFXML);


	}
	/**
	 * This method loads the purchase list screen
	 * @param event
	 * @pre The Back button is pressed from the Vehicle Search screen
	 * @post The Purchase list screen is displayed
	 */

	@FXML
	public void back(ActionEvent event){
		fetchPurchasedVehicles();
	}
	/**
	 * This method loads the vehicle search screen
	 * @param event
	 * @pre The edit button is pressed from the edit form screen
	 * @post The Vehicle search screen is displayed
	 */
	@FXML
	public void editBack(ActionEvent event)
	{
		fetchPurchasedVehicles();
	}

	/**
	 * This method fetches the vehicletype, vehicle specifications and transmission type based on the vehicle class
	 * while adding new vehicles
	 * @param event
	 * @pre vehicle class should be selected
	 * @post list the vehicletypes , vehicle specifications and transmission type
	 */
	@FXML
	public void fetchVehicletype(ActionEvent event){
		if(addVehicleClass.getValue() != null && !addVehicleClass.getValue().isEmpty()){
			PurchseLogic logic = new PurchseLogic();
			if(addVehicleClass.getValue().equalsIgnoreCase(Constants.CAR) || 
					addVehicleClass.getValue().equalsIgnoreCase(Constants.TRUCK)){
				try {
					List<String> vehiceTypeList = logic.fetchVehicleType(addVehicleClass.getValue());
					addVehicleType.getItems().clear();
					addVehicleType.getItems().addAll(vehiceTypeList);
					List<String> vehicleSpecList = new ArrayList<String>();
					if(addVehicleClass.getValue().equalsIgnoreCase(Constants.CAR)){
						vehicleSpecList.add("2-Seater");
						vehicleSpecList.add("4-Seater");
						vehicleSpecList.add("5-Seater");

					}
					else if(addVehicleClass.getValue().equalsIgnoreCase(Constants.TRUCK)){
						vehicleSpecList.add("8-Tons");
						vehicleSpecList.add("10-Tons");
						vehicleSpecList.add("12-Tons");
						vehicleSpecList.add("16-Tons");
					}
					addVehicleSpec.getItems().clear();
					addVehicleSpec.getItems().addAll(vehicleSpecList);

				} catch (SQLException e) {
					addmessage.setText("Fetching vehcile type failed because of DB exception");
					e.printStackTrace();
				}
			}
			else{
				addmessage.setText("Please select a valid vehicle class");				
			}
		}

	}

	/**
	 * This method fetches the vehicletype, vehicle specifications and transmission type based on the vehicle class
	 * while editing
	 * @param event
	 * @pre vehicle class should be selected
	 * @post list the vehicletypes , vehicle specifications and transmission type
	 */


	@FXML
	public void fetchVehicletypeForEdit(ActionEvent event){
		if(editVehicleClass.getValue() != null && !editVehicleClass.getValue().isEmpty()){
			PurchseLogic logic = new PurchseLogic();
			if(editVehicleClass.getValue().equalsIgnoreCase(Constants.CAR) || 
					editVehicleClass.getValue().equalsIgnoreCase(Constants.TRUCK)){
				try {
					List<String> vehiceTypeList = logic.fetchVehicleType(editVehicleClass.getValue());
					editVehicleType.getItems().clear();
					editVehicleType.getItems().addAll(vehiceTypeList);
					List<String> vehicleSpecList = new ArrayList<String>();
					if(editVehicleClass.getValue().equalsIgnoreCase(Constants.CAR)){
						vehicleSpecList.add("2-Seater");
						vehicleSpecList.add("4-Seater");
						vehicleSpecList.add("5-Seater");

					}
					else if(editVehicleClass.getValue().equalsIgnoreCase(Constants.TRUCK)){
						vehicleSpecList.add("8-Tons");
						vehicleSpecList.add("10-Tons");
						vehicleSpecList.add("12-Tons");
						vehicleSpecList.add("16-Tons");
					}
					editVehicleSpec.getItems().clear();
					editVehicleSpec.getItems().addAll(vehicleSpecList);
				} catch (SQLException e) {
					editMessage.setText("Fetching vehcile type failed because of DB exception");	
					e.printStackTrace();
				}

			}
			else{
				editMessage.setText("Please select a valid vehicle class");				
			}
		}

	}
	/**
	 * This method saves the entered value in the add form to the inventory
	 * 
	 * @param event
	 * @throws ParseException
	 * @pre The Add vehicle button is pressed 
	 * @post The Entered valus are saved into the inventory
	 */

	@FXML
	public void addPurchasedVehicle(ActionEvent event) throws ParseException{	
		addmessage.setVisible(false);
		PurchasedVehicleEntity purchasedvehicle = new PurchasedVehicleEntity();
		PurchseLogic purchaselogic = new PurchseLogic();
		try
		{
			if(addVIN.getText().trim().isEmpty())
			{
				addmessage.setText("Please Enter the VIN number");
				addmessage.setVisible(true);
				return;
			}

			if(!purchaselogic.checkVin(addVIN.getText())){
				addmessage.setText("Entered VIN already exists");
				addmessage.setVisible(true);
				return;
			}

			if(addPurchasedDate.getValue() == null)
			{
				addmessage.setText("Please enter the Purchased date");
				addmessage.setVisible(true);
				return;
			}
			if(addPurchasedDate.getValue().isAfter(LocalDate.now())) 
			{
				addmessage.setText("Please enter the Purchased date less "
						+ "than or equal to current date");
				addmessage.setVisible(true);
				return;
			}
			if(addManufDate.getValue() == null)
			{
				addmessage.setText("Please enter the Manufacture date");
				addmessage.setVisible(true);
				return;
			}
			if(addManufDate.getValue().isAfter(LocalDate.now())) 
			{
				System.out.println("future date");
				addmessage.setText("Please enter the Manufacture date less "
						+ "than or equal to current date");
				addmessage.setVisible(true);
				return;
			}
			if(addPurchasedDate.getValue().isBefore(addManufDate.getValue()) )
			{
				System.out.println("before");
				addmessage.setText("Please enter the Purchased date greater "
						+ "than or equal to manufacture date");
				addmessage.setVisible(true);
				return;
			}	
			LocalDate purchaseDate = addPurchasedDate.getValue();
			Instant instant = purchaseDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
			Date res = Date.from(instant);		
			purchasedvehicle.setPurchasedDate(res);

			LocalDate manufDate = addManufDate.getValue();
			Instant instant1 = manufDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
			Date res1 = Date.from(instant1);		
			purchasedvehicle.setManufDate(res1);

			if (addVehicleIns.getText().trim().isEmpty())
			{
				addmessage.setText("Please Enter the Vehicle Insurance amount value");
				addmessage.setVisible(true);
				return;	
			}
			else if(!Validator.validateStringContainsDouble(addVehicleIns.getText().trim().trim()))
			{
				System.out.println("Enter valid entry for vehicle insurance");
				addmessage.setText("Enter valid entry for vehicle insurance");
				addmessage.setVisible(true);
				return;
			}
			if (addAmount.getText().trim().isEmpty())
			{
				addmessage.setText("Please Enter the Purchased amount value");
				addmessage.setVisible(true);
				return;	
			}
			else if(!Validator.validateStringContainsDouble(addAmount.getText().trim()))
			{
				System.out.println("Enter valid entry for Purchased Amount");
				addmessage.setText("Enter valid entry for Purchased Amount");
				addmessage.setVisible(true);
				return;
			}
			if(addVehicleName.getText().trim().isEmpty())
			{
				addmessage.setText("Please Enter the Vehicle Name");
				addmessage.setVisible(true);
				return;
			}
			else if (addRegNo.getText().trim().isEmpty())
			{
				addmessage.setText("Please Enter the RegNo");
				addmessage.setVisible(true);
				return;	
			}

			double vehicleIns = Validator.formatDouble(Double.parseDouble(addVehicleIns.getText().trim()));
			double addamount  = Validator.formatDouble(Double.parseDouble(addAmount.getText().trim()));

			purchasedvehicle.setVehicleIns(vehicleIns);
			purchasedvehicle.setAmount(addamount);
			//purchasedvehicle.setVehicleIns(new Double(addVehicleIns.getText().trim()));
			//purchasedvehicle.setAmount(new Double(addAmount.getText().trim()));
			purchasedvehicle.setVehicleName(addVehicleName.getText().trim());
			purchasedvehicle.setRegNo(addRegNo.getText().trim());
			purchasedvehicle.setVin(addVIN.getText().trim());
			if(addVehicleClass.getValue() == null || StringUtils.isEmpty(addVehicleClass.getValue())){
				addmessage.setText("Please Select Vehicle Class");
				addmessage.setVisible(true);
				return;
			}
			if(addVehicleType.getValue() == null || StringUtils.isEmpty(addVehicleType.getValue())){
				addmessage.setText("Please Select Vehicle Class");
				addmessage.setVisible(true);
				return;
			}
			if(addVehicleSpec.getValue() == null || StringUtils.isEmpty(addVehicleSpec.getValue())){
				addmessage.setText("Please Select Vehicle Specification");
				addmessage.setVisible(true);
				return;
			}
			if(addVehicleTransmission.getValue() == null || StringUtils.isEmpty(addVehicleTransmission.getValue())){
				addmessage.setText("Please Select Vehicle Transmission Type");
				addmessage.setVisible(true);
				return;
			}
			purchasedvehicle.setVehicleClass(addVehicleClass.getValue());

			purchasedvehicle.setVehicleType(addVehicleType.getValue());

			purchasedvehicle.setSpec(addVehicleSpec.getValue());
			purchasedvehicle.setTransmission(addVehicleTransmission.getValue());

			String msg = 	purchaselogic.addPurchasedVehicle(purchasedvehicle);
			System.out.println(msg+"in controller");
			if(msg!=null)
			{
				addmessage.setTextFill(Color.GREEN);
				addmessage.setText(msg);
				addmessage.setVisible(true);
				return;
			}

		}
		catch(SQLException e1){
			addmessage.setTextFill(Color.RED);
			addmessage.setText("DB exception occurred. Please try again later.");
			addmessage.setVisible(true);
			return;
		}
	}
	/**
	 * This method maps the valus into the table and the input field 
	 * @pre The method is initialized whenever the purchase vehicle controller is invoked
	 * @post sets the values to the table and input field
	 */
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		if(page == -2)
			if(Navigator.isInitailize()){
				System.out.println("inside initialize");
				vehicleId.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleDisplay,Integer>("vehicleId"));
				purchasedDate.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleDisplay,Date>("purchasedDate"));
				manufDate.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleDisplay,Date>("manufDate"));
				vehicleIns.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleDisplay,Double>("vehicleIns"));
				amount.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleDisplay,Double>("amount"));
				VehicleName.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleDisplay,String>("VehicleName"));
				RegNo.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleDisplay,String>("RegNo"));
				VIN.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleDisplay,String>("VIN"));
				vehicleClass.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleDisplay,String>("vehicleClass"));
				vehicleType.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleDisplay,String>("vehicleType"));

				purchaselist.setItems(list);

				Navigator.setInitailize(false);
			}

		if(page==3 && purchaseentity!= null){

			System.out.println("Setting value");
			System.out.println(purchaseentity.getPurchasedDate().toString());
			System.out.println(purchaseentity.getPurchasedDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
			editRegNo.setText(purchaseentity.getRegNo());
			editPurchasedDate.setVisible(true);
			editPurchasedDate.setValue(LocalDate.now());
			editPurchasedDate.setValue(purchaseentity.getPurchasedDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
			editManufDate.setValue(purchaseentity.getManufDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
			editVehicleIns.setText(new Double(purchaseentity.getVehicleIns()).toString());
			editAmount.setText(new Double(purchaseentity.getAmount()).toString());
			editRegNo.setText(purchaseentity.getRegNo());
			editVIN.setText(purchaseentity.getVin());
			editVehicleType.setValue(purchaseentity.getVehicleType());
			editVehicleClass.setValue(purchaseentity.getVehicleClass());
			editVehicleSpec.setValue(purchaseentity.getSpec());
			editVehicleTransmission.setValue(purchaseentity.getTransmission());
			editVehicleName.setText(purchaseentity.getVehicleName());			


		}
		// super dude thanku very muchi am the king :D :D 
		// yeah correct ya
		// dude one min i ll cal nowsure bhai

		if(page==1)
		{
			if(pvd!=null)
				searchvehicleId.setText(pvd.getVehicleId()+"");
			else
			{ System.out.println("Purchase entity nulllll");
			searchvehicleId.setText("");
			}
			Validator.addTextLimiter(searchvehicleId, 06);	
		}
		else if(page==2)
		{
			Validator.addTextLimiter(addVehicleIns, 6);
			Validator.addTextLimiter(addAmount, 7);
			Validator.addTextLimiter(addVehicleName,15 );
			Validator.addTextLimiter(addRegNo,8 );
			Validator.addTextLimiter(addVIN,15 );
		}
		else if(page==3)
		{
			Validator.addTextLimiter(editVehicleIns, 6);
			Validator.addTextLimiter(editAmount, 7);
			Validator.addTextLimiter(editVehicleName,15 );
			Validator.addTextLimiter(editRegNo,8 );
			Validator.addTextLimiter(editVIN,15 );
			//Validator.addTextLimiter(editVehicleClass,5 );
			//Validator.addTextLimiter(editVehicleType,15 );
		}
	}
}
